{extend name="lib/common_list" /}
{block name="title"}角色管理{/block}
{block name="css"}{/block}
{block name="javascript"}
    <script>
        function initTable() {
            //先销毁表格
            $('#tb').bootstrapTable('destroy');
            //初始化表格,动态从服务器加载数据
            $("#tb").bootstrapTable({
                method: "Post",  //使用Post请求到服务器获取数据
                url: "{:url('role/getList')}", //获取数据的Servlet地址
                striped: true,  //表格显示条纹
                pagination: true, //启动分页
                pageSize: 20,  //每页显示的记录数
                pageNumber: 1, //当前第几页
                pageList: [20, 50],  //记录数可选列表
                search: false,  //是否启用查询
                sortable: true, //是否启用排序
                sortOrder: "asc", //排序方式
                //showColumns: true,  //显示下拉框勾选要显示的列
                //showRefresh: true,  //显示刷新按钮
                sortable: true,
                //sortOrder: "desc",
                //sortName: "PullTime",
                sidePagination: "server", //表示服务端请求
                //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
                //设置为limit可以获取limit, offset, search, sort, order
                queryParamsType: "limit",
                contentType: "application/x-www-form-urlencoded",
                queryParams: function queryParams(params) {   //设置查询参数
                    var param = {
                        limit: params.limit,
                        offset: params.offset,
                        sortName: params.sort,
                        sortOrder: params.order,
                        keyword: $("input[name=keyword]").val()                      
                    };
                    return param;
                },
                onLoadSuccess: function () {  //加载成功时执行
                    $('.show_image').hover(
                        function(){
                            $(this).next().css('display','block');
                        },
                        function(){
                            $(this).next().css('display','none');
                        }
                    );
                },
                onLoadError: function () {  //加载失败时执行
                    layer.msg("加载数据失败", { time: 1500, icon: 2 });
                },
                rowStyle: function (row, index) {
                    var strclass = "";
                    if (row.isdisable==1) {
                        strclass = 'danger';
                    }                
                    else {
                        return {};
                    }
                    return { classes: strclass }
                }
            });
        }
        
        $(document).ready(function () {
            //调用函数，初始化表格
            initTable();
            //当点击查询按钮的时候执行
            $("#search").bind("click", initTable);
            $("#btnAdd").bind("click", add);
        });

        {if check_permissioncode("1001002002")}
        var layer_add_index;
        function add() {
            layer_add_index = layer.open({
                type: 2,
                title: "新增角色",
                area:['100%', '100%'],
                content:"{:url('role/add')}"
            })
        }
        {/if}

        {if check_permissioncode("1001002003")}
        var layer_edit_index;
        function edit(id) {
            layer_edit_index = layer.open({
                type: 2,
                title: "修改角色",
                area:['100%', '100%'],
                content:"{:url('role/edit')}?id="+id
            })
        }
        {/if}

        function operateFormatter(index, row) {
            var html = "";
            if (!row.is_manager) {
                {if check_permissioncode("1001002003")}
                    html += "<a href=\"javascript:edit(" + row.id + ");\"><i class=\"fa fa-edit text-navy\"></i>修改</a>";
                {/if}
                {if check_permissioncode("1001002004")}
                    html += "&nbsp;&nbsp;<a href=\"javascript:del(" + row.id + ");\"><i class=\"fa fa-times text-danger\"></i>删除</a>";
                {/if}
            }
            {if check_permissioncode("1001002005")}
            html += "&nbsp;&nbsp;<a href=\"javascript:Roleuser(" + row.id + ");\"><i class=\"fa fa fa-male text-navy\"></i>成员列表</a>";
            {/if}
            return html;
        }

        {if check_permissioncode("1001002004")}
        function del(id) {
            layer.confirm('你确定要删除该角色吗？', {
                btn: ['是', '否'] //按钮
            }, function () {
                var load_index = layer.load(0, { shade: false });
                $.ajax({
                    type: "post",
                    url: "{:url('role/del')}",
                    data: { id: id },
                    dataType: "json",
                    success: function (result) {
                        layer.close(load_index);
                        if (result.code == 0) {
                            var ly = layer.alert('删除成功！', function () {
                                initTable();
                                layer.close(ly);
                            });
                        } else {
                            layer.alert(result.msg);
                        }
                    },
                    error:function(xhr,textStatus){
                        layer.close(load_index);
                        layer.alert('服务器繁忙！');
                    }
                });
            });
        }
        {/if}

        function dateFormat(index, row) {
            if(index==0) return "";
            var newDate = new Date();
            newDate.setTime(index * 1000);
            return newDate.format('yyyy-MM-dd h:m:s');
        }

        {if check_permissioncode("1001002005")}
        var layer_roleuser_index;
        function Roleuser(id) {
            layer_roleuser_index = layer.open({
                type: 2,
                title: "角色管理",
                area: ['100%', '100%'],
                content: "{:url('role/roleuser')}?roleid="+id
            })
        }
        {/if}
    </script>
{/block}
{block name="content"}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>角色列表</h5>
                    </div>
                    <div class="ibox-content">
                        <div class="row">
                            <div class="col-sm-2 m-b-xs">
                                {if check_permissioncode("1001002002")}
                                <span>
                                    <button type="button" class="btn btn-primary btn-sm" id="btnAdd"><i class="glyphicon glyphicon-plus"></i> 新增</button>
                                </span>
                                {/if}
                            </div>                        
                            <div class="col-sm-2 col-sm-offset-2">
                                <div class="input-group">
                                    <span class="input-group-btn">
                                        <input type="text" placeholder="请输入关键词" class="input-sm form-control" id="keyword" name="keyword">
                                        <button type="button" class="btn btn-sm btn-primary" id="search">搜索</button>
                                    </span>
                                </div>
                            </div>
                            <span id="refresh" class="fa fa-refresh fa-spin" title="刷新" onclick="javascript:$('#tb').bootstrapTable('refresh')"></span>
                        </div>
                        <table data-toggle="table" id="tb" data-pagination="true">
                            <thead>
                                <tr>
                                    <th data-align="center" data-field="role_name">角色名称</th>
                                    <th data-align="center" data-field="remark">描述</th>                                    
                                    <th data-align="center" data-field="create_time">创建时间</th>
                                    <th data-align="center" data-formatter="operateFormatter">操作</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
{/block}
